package com.maochang.top.config;


import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBaitsConfig {

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setTypeAliasesPackage("com.maochang.top.bean");
        sqlSessionFactoryBean.setDataSource(dataSource);
        //sqlSessionFactoryBean.setConfiguration(configuration);
        //1.配置myBatis配置文件
        //sqlSessionFactoryBean.setConfigLocation();
        //2.配合sql.xml扫描文件
        //sqlSessionFactoryBean.setMapperLocations(classpath:mapper/*.xml);
        return  sqlSessionFactoryBean;
    }


    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc=new MapperScannerConfigurer();
        msc.setBasePackage("com.maochang.top.dao");
        return  msc;
    }

    @Bean
    public Configuration configuration(){
        Configuration configuration=new Configuration();
        //允许 JDBC 支持自动生成主键，需要数据库驱动支持。如果设置为 true，将强制使用自动生成主键。尽管一些数据库驱动不支持此特性，但仍可正常工作（如 Derby）
        configuration.setUseGeneratedKeys(true);
        //是否开启自动驼峰命名规则（camel case）映射
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(Slf4jImpl.class);
        return  configuration;
   }


}
